package 算法.leetcode.bytedance;

import java.util.Stack;

/**
 * @author lchenglong
 * @date 2022/5/7
 */
public class OfferString_VI {

    public static void main(String[] args) {
        OfferString_VI instance = new OfferString_VI();
        String ans = instance.simplifyPath("/a/../../b/../c//.//");
        System.out.println(ans);
    }
    public String simplifyPath(String path) {
        String[] array = path.split("\\/");
        Stack<String> stack = new Stack<>();
        for (String s:array){

            if (s.equals(".")||s.equals(""))continue;
            if (s.equals("..")) {
                if (!stack.empty()) stack.pop();
                continue;
            }
            stack.push(s);
        }
        if (stack.empty())return "/";
        StringBuilder ans = new StringBuilder();
        while (!stack.empty()){
            ans.insert(0,"/" +stack.pop());
        }
//        ans.insert(0,"/");
        return ans.toString();

    }
}
